package com.lpf.json.fastjson.jsonpath;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONPath;

@SuppressWarnings("rawtypes")
public class TestJSONPath {

	@SuppressWarnings("unchecked")
	public static void main(String[] args) {

		List<Entity> entities = new ArrayList<Entity>();

		Entity entity1 = new Entity(1, "挖坑埋你1", "CSDN1");
		Entity entity2 = new Entity(2, "挖坑埋你2", "CSDN2");
		Entity entity3 = new Entity(3, "挖坑埋你3", "CSDN3");

		entities.add(entity1);
		entities.add(entity2);
		entities.add(entity3);

		
		System.out.println("\n------读取对象中的值---------------------------------------------------------");
		System.out.println("JSONPath.eval(entity, \"$.value\") = " + JSONPath.eval(entity1, "$.value"));
		System.out.println("JSONPath.contains(entity, \"$.value\") = " + JSONPath.contains(entity1, "$.value"));
		System.out.println("JSONPath.containsValue(entity, \"$.id\", 123) = " + JSONPath.containsValue(entity1, "$.id", 123));
		System.out.println("JSONPath.containsValue(entity, \"$.value\", entity.getValue()) = " + JSONPath.containsValue(entity1, "$.value", entity1.getValue()));
		System.out.println("JSONPath.size(entity, \"$\") = " + JSONPath.size(entity1, "$"));
		System.out.println("JSONPath.size(new Object[], \"$\") = " + JSONPath.size(new Object[5], "$"));

		
		System.out.println("\n------读取集合多个元素的某个属性：JSONPath.eval(entities, \"$.name\")-------------");
		List<String> names = (List<String>) JSONPath.eval(entities, "$.name"); // 返回enties的所有名称
		System.out.println("$.name = " + names);
		
		
		System.out.println("\n------返回集合中多个元素：JSONPath.eval(entities, \"[1,2]\")--------------------");
		List<Entity> result = (List<Entity>) JSONPath.eval(entities, "[1,2]"); // 返回下标为1和2的元素
		System.out.println("[1,2] = " + result);
		
		
		System.out.println("\n------按范围返回集合的子集：JSONPath.eval(entities, \"[0:2]\")-------------------");
		result = (List<Entity>) JSONPath.eval(entities, "[0:2]"); // 返回下标从0到2的元素
		System.out.println("[0:2] = " + result);

		
		System.out.println("\n------通过条件过滤，返回集合的子集：JSONPath.eval(entities, \"[id in (1)]\")-------");
		result = (List<Entity>) JSONPath.eval(entities, "[id in (1)]");
		System.out.println("[id in (1)] = " + result);

		
		System.out.println("\n------根据属性值过滤条件判断是否返回对象--------------------------------------------");
		System.out.println("[id = 1] = " + JSONPath.eval(entity1, "[id = 1]"));
		System.out.println("[id = 1002] = " + JSONPath.eval(entity1, "[id = 1002]"));

		
		System.out.println("\n------修改对象：JSONPath.set(entity1, \"id\", 123456);//将id字段修改为123456-----");
		JSONPath.set(entity1, "id", 123456); // 将id字段修改为123456
		System.out.println("entity1.getId().intValue()=" + entity1.getId().intValue());

		
		System.out.println("\n------集合或数组属性添加元素-----------------------------------------------------");
		JSONPath.set(entity1, "value", new ArrayList<Integer>()); // 将value字段赋值为长度为0的集合
		System.out.println("entity1=" + entity1);

		JSONPath.arrayAdd(entity1, "value", 1, 2, 3); // 将value字段的集合添加元素1,2,3
		System.out.println("entity1=" + entity1);

		
		System.out.println("\n------操作复杂对象：JSONPath.eval(root, \"$..id\")-----------------------------");
		Map root = Collections.singletonMap("company", //
				Collections.singletonMap("departs", //
						Arrays.asList( //
								Collections.singletonMap("id", 1001), //
								Collections.singletonMap("id", 1002), //
								Collections.singletonMap("id", 1003) //
						) //
				));

		List<Object> ids = (List<Object>) JSONPath.eval(root, "$..id");
		System.out.println("$..id = " + ids);
	}
}
